package com.tao.com.xiaowen.config.http;

import com.tao.com.xiaowen.model.JokeData;
import com.tao.com.xiaowen.model.News;
import com.tao.com.xiaowen.model.Picture;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/8/3.
 */
public class OkHttpHelper {
    private OkHttpClient mOkHttpClient;
    private static OkHttpHelper mInstance;

    private OkHttpHelper() {
        mOkHttpClient = new OkHttpClient.Builder()
                .readTimeout(30, TimeUnit.SECONDS)//读取超时
                .connectTimeout(10, TimeUnit.SECONDS)//连接超时
                .build();

    }

    public static OkHttpHelper getInstance() {
        if (mInstance == null) {
            synchronized (OkHttpHelper.class) {
                if (mInstance == null) {
                    mInstance = new OkHttpHelper();
                }
            }
        }

        return mInstance;
    }

    /**
     * get方法获取实体
     */

    public void getExecute(String url) {
        final Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                List<JokeData> list = MyJson.getJokeJsonList(result);
                if (list != null) {
                    for (JokeData jokeData : list) {
                        jokeData.setPicture(jokeData.getUrl());
                        jokeData.save(new SaveListener<String>() {
                            @Override
                            public void done(String s, BmobException e) {

                            }
                        });
                    }
                }
            }
        });
    }
    public void getNewsExecute(String url) {
        final Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                List<News> list = MyJson.getNewsJsonList(result);
                if (list != null) {
                    for (News newsData : list) {
                        newsData.save(new SaveListener<String>() {
                            @Override
                            public void done(String s, BmobException e) {

                            }
                        });
                    }
                }
            }
        });
    }
    public void getPicExecute(String url) {
        final Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                List<Picture> list = MyJson.getPictureJsonList(result);
                if (list != null) {
                    for (Picture picData : list) {
                        picData.save(new SaveListener<String>() {
                            @Override
                            public void done(String s, BmobException e) {

                            }
                        });
                    }
                }
            }
        });
    }


    /**
     * get方法连接拼加参数
     */

    private String setUrlParams(Map<String, String> mapParams) {
        String strParams = "";
        if (mapParams != null) {
            Iterator<String> iterator = mapParams.keySet().iterator();
            String key = "";
            while (iterator.hasNext()) {
                key = iterator.next().toString();
                strParams += "&" + key + "=" + mapParams.get(key);
            }
        }

        return strParams;
    }
}
